#!/usr/bin/python

import subprocess,sys

def exe(cmds,printfunc=sys.stdout.write):
	#?? do this with pty such that child process output is unbuffered
	cmds=[s.strip() for s in cmds.split('\n')]
	fullout=''
	for i in range(len(cmds)):
		if cmds[i]=='':continue
		if printfunc: printfunc('\n## %s\n'%cmds[i])
		p = subprocess.Popen(cmds[i],shell=True,stdout=subprocess.PIPE,stderr=subprocess.STDOUT)
		out,__=p.communicate()
		if printfunc: printfunc(out)
		assert not p.returncode,('process exited with an error',cmds[i],p.returncode,out)
		fullout+=out
	return fullout